package com.lanlian.cayman.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.KLineHengPingActivity;
import com.lanlian.cayman.fragment.MarketFragment;
import com.lanlian.utillib.PinYinUtil;
import com.lanlian.commonlib.bean.db.DBMarketSerch;
import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.commonlib.data.ContractBean;
import com.lanlian.commonlib.data.RespMarket;
import com.lanlian.utillib.DisplayUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 *     date   : 2018/8/22 20:09
 *     desc   : 选择商品对话框
 *     version: 1.0
 * </pre>
 */
public class GoodsSelectDialog extends Dialog implements View.OnClickListener {

    private KLineHengPingActivity mContext;
    /***
     * 搜索历史
     */
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private EditText et;
    private ImageView iv;
    private ImageView iv_delete;
    private TextView mtv_msg;
    private LinearLayout ll_text;
    private LinearLayout ll_lishi;
    private LinearLayout ll_nodata;
    ListView lv;
    ArrayList<ContractBean> mList;
    List<DBMarketSerch> mMarket;

    public GoodsSelectDialog(@NonNull Context context) {
        super(context, R.style.customDialog);
        this.mContext = (KLineHengPingActivity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_goodsselect);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        // lp.width = (int) (DisplayUtil.getScreenWidth(mContext) * 0.6);
        lp.width = DisplayUtil.dip2px(mContext, 200);
        lp.height = lp.MATCH_PARENT;
        lp.gravity = Gravity.LEFT;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        getWindow().setAttributes(lp);

        //ViewGroup.LayoutParams params = getWindow().getAttributes();
        //params.width = (int) (DisplayUtil.getScreenWidth(mContext) * 0.4);
        RespMarket rm = MarketFragment.getRespMarket();
        if (rm != null) {
            mList = rm.getContractList();
        }
        initView();

    }

    private void initView() {
        mMarket = DBManager.getInstance().getMarketSearch();
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        lv = findViewById(R.id.lv);
        et = findViewById(R.id.et);
        iv = findViewById(R.id.iv);
        iv_delete = findViewById(R.id.iv_delete);
        iv_delete.setOnClickListener(this);
        mtv_msg = findViewById(R.id.tv_msg);
        ll_lishi = findViewById(R.id.ll_lishi);
        ll_text = findViewById(R.id.ll_text);
        ll_nodata = findViewById(R.id.ll_nodata);

        et.addTextChangedListener(new GoodsSelectDialog.TextWatcherListener());

        iv.setOnClickListener(this);

        initData();
    }


    private void initData() {
        ArrayList<ContractBean> list = mList;
        if (list == null || list.size() == 0) {
            ll_lishi.setVisibility(View.GONE);
            mtv_msg.setVisibility(View.GONE);
            ll_text.setVisibility(View.GONE);
            ll_nodata.setVisibility(View.VISIBLE);
        } else {
            ll_lishi.setVisibility(View.GONE);
            mtv_msg.setVisibility(View.GONE);
            ll_text.setVisibility(View.GONE);
            ll_nodata.setVisibility(View.GONE);
            addData(list);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.iv:
                DBManager.getInstance().deleteAllMarketSearch();
                initView();
                break;
            case R.id.iv_delete:
                et.setText("");
                break;
            default:
                break;
        }
    }

    private ArrayList<ContractBean> select(String str) {
        str = str.toLowerCase();
        ArrayList<ContractBean> list = new ArrayList<>();
        ContractBean cb = null;
        if (mList == null) {
            return list;
        }
        //文字搜索
        for (int i = 0; i < mList.size(); i++) {
            cb = mList.get(i);
            //文字搜索
            if (cb.contractName.toLowerCase().contains(str)) {
                list.add(cb);
            }
            //代码
            else if (cb.contractNo.toLowerCase().contains(str)) {
                list.add(cb);
            }
            //全拼
            else if (PinYinUtil.getFullSpell(cb.contractName.toLowerCase()).contains(str)) {
                list.add(cb);
            }
            //首字母搜索
            else if (PinYinUtil.getFirstSpell(cb.contractName.toLowerCase()).contains(str)) {
                list.add(cb);
            }
        }


        return list;
    }


    class TextWatcherListener implements TextWatcher {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (!TextUtils.isEmpty(s.toString())) {
                ArrayList<ContractBean> list = select(s.toString());
                if (s.length() > 0) {
                    iv_delete.setVisibility(View.VISIBLE);
                } else {
                    iv_delete.setVisibility(View.GONE);
                }
                if (list.size() == 0) {

                    ll_lishi.setVisibility(View.GONE);
                    mtv_msg.setVisibility(View.GONE);
                    ll_text.setVisibility(View.GONE);

                    ll_nodata.setVisibility(View.VISIBLE);
                } else {
                    lv.setVisibility(View.VISIBLE);
                    ll_lishi.setVisibility(View.GONE);
                    mtv_msg.setVisibility(View.GONE);
                    ll_text.setVisibility(View.GONE);
                    ll_nodata.setVisibility(View.GONE);
                    addData(list);
                }
            } else {
                iv_delete.setVisibility(View.GONE);
                ll_lishi.setVisibility(View.GONE);
                mtv_msg.setVisibility(View.GONE);
                ll_text.setVisibility(View.GONE);

                ll_nodata.setVisibility(View.VISIBLE);
                setLishi();
            }
        }
    }

    private void addData(ArrayList<ContractBean> list) {
        ArrayList<Map<String, String>> lists = new ArrayList<>();
        Map<String, String> map;
        for (int i = 0; i < list.size(); i++) {
            map = new HashMap<>();
            map.put("name", list.get(i).contractName);
            map.put("id", list.get(i).contractNo);
            lists.add(map);
        }
        String[] from = {"name", "id"};

        int[] to = {R.id.tv_name, R.id.tv_id};

        SimpleAdapter adapter = new SimpleAdapter(mContext, lists, R.layout.market_search_item2, from, to) {
        };
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView tv_name = view.findViewById(R.id.tv_name);
                TextView tv_id = view.findViewById(R.id.tv_id);

                DBMarketSerch ms = new DBMarketSerch();
                ms.setId(tv_id.getText().toString());
                ms.setName(tv_name.getText().toString());
                DBManager.getInstance().setMarketSearch(ms);
                mContext.id = tv_id.getText().toString();
                mContext.name = tv_name.getText().toString();
                mContext.reData();
                dismiss();
            }
        });
    }

    private void setLishi() {
        lv.setVisibility(View.GONE);
        if (mMarket == null || mMarket.size() == 0) {
            ll_text.setVisibility(View.INVISIBLE);
            ll_lishi.setVisibility(View.GONE);
            mtv_msg.setVisibility(View.VISIBLE);
            ll_nodata.setVisibility(View.GONE);
        } else {
            ll_lishi.setVisibility(View.VISIBLE);
            mtv_msg.setVisibility(View.GONE);
            ll_text.setVisibility(View.VISIBLE);
            ll_nodata.setVisibility(View.GONE);
            for (int i = 0; i < 3; i++) {
                if (i == 0) {
                    if (mMarket.size() - 1 >= 0) {
                        tv1.setText(mMarket.get(mMarket.size() - 1).getName());
                        tv1.setVisibility(View.VISIBLE);
                        tv1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                next(mMarket.get(mMarket.size() - 1).getId(), mMarket.get(mMarket.size() - 1).getName());
                            }
                        });
                    } else {
                        tv1.setVisibility(View.GONE);
                    }
                } else if (i == 1) {
                    if (mMarket.size() - 2 >= 0) {
                        tv2.setText(mMarket.get(mMarket.size() - 2).getName());
                        tv2.setVisibility(View.VISIBLE);
                        tv2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                next(mMarket.get(mMarket.size() - 2).getId(), mMarket.get(mMarket.size() - 2).getName());
                            }
                        });
                    } else {
                        tv2.setVisibility(View.INVISIBLE);
                    }
                } else if (i == 2) {
                    if (mMarket.size() - 3 >= 0) {
                        tv3.setText(mMarket.get(mMarket.size() - 3).getName());
                        tv3.setVisibility(View.VISIBLE);
                        tv3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                next(mMarket.get(mMarket.size() - 3).getId(), mMarket.get(mMarket.size() - 3).getName());
                            }
                        });
                    } else {
                        tv3.setVisibility(View.INVISIBLE);
                    }
                }
            }
        }
    }

    private void next(String id, String name) {


        DBMarketSerch ms = new DBMarketSerch();
        ms.setId(id);
        ms.setName(name);
        DBManager.getInstance().setMarketSearch(ms);
        mContext.id = id;
        mContext.name = name;
        mContext.reData();
        dismiss();
    }

    public void showDialog() {
        show();
    }

}
